package com.wsc.wisdomchina.activitity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.JsonTools;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchina.activitity.wisdomParty.PartyLearnListActivity;
import com.wsc.wisdomchina.callback.ContributorView;
import com.wsc.wisdomchina.constant.Constant;
import com.wsc.wisdomchina.fragment.PartyAnounceFragment;
import com.wsc.wisdomchina.fragment.PartyNewsFragment;
import com.wsc.wisdomchina.fragment.SelectedIndustryFragment;
import com.wsc.wisdomchina.javaBean.HomeBannerDetail;
import com.wsc.wisdomchina.javaBean.LookListDetailBean;
import com.wsc.wisdomchina.javaBean.MessageListDetailBean;
import com.wsc.wisdomchina.javaBean.RespObj;
import com.wsc.wisdomchina.javaBean.ShangjiaCXDetail;
import com.wsc.wisdomchina.javaBean.wisdomParty.PartyBulletinListBean;
import com.wsc.wisdomchina.javaBean.wisdomParty.PartyLearnBean;
import com.wsc.wisdomchina.javaBean.wisdomParty.PartyNewsListBean;
import com.wsc.wisdomchina.javaBean.wisdomParty.PartyUserBean;
import com.wsc.wisdomchina.presenter.ContributorPresenter;

public class InfoDetailActivity extends BaseActivity implements View.OnClickListener, ContributorView {
    private ImageView regist_back;
    private TextView tv_title;
    private TextView tv_time;
    private Context context;
    private ContributorPresenter contributorPresenter;
    private WebView webView;
    private TextView tv_classname;
    private String flag;
    private TextView tv_headtitle;
    private TextView fragment_zxxq_seecount;
    private ImageView iv_shareicon;
    private String idIntent;
    private String titleIntent;
    private LinearLayout layout_learnconnect;
    private LinearLayout learn_layout;
    private TextView tv_learn;
    private TimeCount registerCheck;
    private PartyLearnBean partyLearnBean;
    private final String POST_LEARNSCHEDULE="POST_LEARNSCHEDULE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.infodetail_layout);
        initView();
        context = this;
        contributorPresenter = new ContributorPresenter(this, this);
        Intent intent=getIntent();
        idIntent= intent.getStringExtra("id");
        flag = intent.getStringExtra("flag");
        titleIntent=intent.getStringExtra("title");
        if(idIntent==null)
            idIntent="";
        if(flag==null)
            flag="";
        if(titleIntent==null)
            titleIntent="";
        //资讯详情
        if(flag.equals(SelectedIndustryFragment.class.getSimpleName()))
        {
            fragment_zxxq_seecount.setVisibility(View.VISIBLE);
            contributorPresenter.getLookListDetail(idIntent, flag);
            iv_shareicon.setVisibility(View.VISIBLE);
        }
        //banner详情
        else if(flag.equals("BannerDetailFlag"))
        {
            fragment_zxxq_seecount.setVisibility(View.VISIBLE);
            contributorPresenter.getHomeBannerDetail(idIntent, flag);
        }
        //商家促销详情
        else if(flag.equals(ShangjiaCuXiaoActivity.class.getSimpleName()))
        {
            fragment_zxxq_seecount.setVisibility(View.VISIBLE);
            contributorPresenter.getBusinessPromotionDetail(idIntent,flag);
        }
        //消息
        else if(flag.equals(MessageActivity.class.getSimpleName()))
        {
            tv_headtitle.setText(titleIntent);
            contributorPresenter.getMessageDetail(idIntent,flag);
        }
        //党建活动公告通知详情
        else if(flag.equals(PartyAnounceFragment.class.getSimpleName()))
        {
            contributorPresenter.getNoticeOnClickBulletin(idIntent,flag);
        }
        //党建资讯
        else if(flag.equals(PartyNewsFragment.class.getSimpleName()))
        {
            contributorPresenter.getPartyInfomationDetail(idIntent,flag);
        }else if(flag.equals(PartyLearnListActivity.class.getSimpleName()))
        {
            //党员学习列表详情
            tv_headtitle.setText("详情");
            fragment_zxxq_seecount.setVisibility(View.VISIBLE);
            learn_layout.setVisibility(View.VISIBLE);
            layout_learnconnect.setVisibility(View.VISIBLE);
            String stringjson=Tools.getSharedPreferencesStrings(Constant.SharePreferencePartyUserInfo,"",context);
            PartyUserBean partyUserBean=JsonTools.jsonStringToObject(stringjson,PartyUserBean.class);
            contributorPresenter.getLearnContentOne(idIntent,partyUserBean.getId(),flag);
        }

    }

    private void initView() {
        regist_back = (ImageView) findViewById(R.id.regist_back);
        regist_back.setOnClickListener(this);
        tv_title = (TextView) findViewById(R.id.fragment_zxxq_name);
        tv_time = (TextView) findViewById(R.id.fragment_zxxq_time);
        webView = (WebView) findViewById(R.id.fragment_zxxq_webView);
        tv_classname = (TextView) findViewById(R.id.fragment_zxxq_className);
        tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
        if(TextUtils.isEmpty(titleIntent)){
            tv_headtitle.setText("资讯详情");
        }else{
            tv_headtitle.setText(titleIntent);
        }
        fragment_zxxq_seecount=(TextView) findViewById(R.id.fragment_zxxq_seecount);
        iv_shareicon=(ImageView) findViewById(R.id.iv_shareicon);
        iv_shareicon.setOnClickListener(this);
        layout_learnconnect=(LinearLayout) findViewById(R.id.layout_learnconnect);
        learn_layout=(LinearLayout) findViewById(R.id.learn_layout);
        tv_learn=(TextView) findViewById(R.id.tv_learn);
    }


    @Override
    public void onLoadContributorStart(String flag) {

    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            if (flag.equals(SelectedIndustryFragment.class.getSimpleName())) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String messageJson = JsonTools.objectToJsonString(object);
                    LookListDetailBean newsDetailBean = JsonTools.jsonStringToObject(messageJson, LookListDetailBean.class);
                    tv_title.setText(newsDetailBean.getTitle());
                    String date_time = Tools.timedate(String.valueOf(newsDetailBean.getCrt() / 1000));
                    tv_time.setText(date_time);
                    tv_classname.setVisibility(View.VISIBLE);
                    tv_classname.setText(newsDetailBean.getTypeName());
                    String Url = Constant.Head_string + newsDetailBean.getContent() + Constant.foot_string;//URL编码数据
                    webView.getSettings().setDefaultTextEncodingName("UTF-8");
                    webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                    webView.getSettings().setLoadWithOverviewMode(false);
                    webView.loadData(Url, "text/html; charset=UTF-8", null);
                    fragment_zxxq_seecount.setText(newsDetailBean.getReadCount()+"");
                }
            }else if(flag.equals("BannerDetailFlag"))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String messageJson = JsonTools.objectToJsonString(object);
                    HomeBannerDetail homeBannerDetail = JsonTools.jsonStringToObject(messageJson, HomeBannerDetail.class);
                    tv_title.setText(homeBannerDetail.getTitle());
                    String Url = Constant.Head_string + homeBannerDetail.getContent() + Constant.foot_string;//URL编码数据
                    webView.getSettings().setDefaultTextEncodingName("UTF-8");
                    webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                    webView.getSettings().setLoadWithOverviewMode(false);
                    webView.loadData(Url, "text/html; charset=UTF-8", null);
                    fragment_zxxq_seecount.setText(homeBannerDetail.getReadCount()+" 阅");
                    String date_time = Tools.timesTwo(String.valueOf(homeBannerDetail.getCrt() / 1000));
                    tv_time.setText(date_time);
                }
            }else if(flag.equals(ShangjiaCuXiaoActivity.class.getSimpleName()))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String messageJson = JsonTools.objectToJsonString(object);
                    ShangjiaCXDetail homeBannerDetail = JsonTools.jsonStringToObject(messageJson, ShangjiaCXDetail.class);
                    tv_title.setText(homeBannerDetail.getTitle());
                    String date_time = Tools.timesTwo(String.valueOf(homeBannerDetail.getCreateDate() / 1000));
                    tv_time.setText(date_time);
                    String Url = Constant.Head_string + homeBannerDetail.getContent() + Constant.foot_string;//URL编码数据
                    webView.getSettings().setDefaultTextEncodingName("UTF-8");
                    webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                    webView.getSettings().setLoadWithOverviewMode(false);
                    webView.loadData(Url, "text/html; charset=UTF-8", null);
                    fragment_zxxq_seecount.setText(homeBannerDetail.getReadCount()+" 阅");
                }
            } else if (flag.equals(MessageActivity.class.getSimpleName())) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String messageJson = JsonTools.objectToJsonString(object);
                    MessageListDetailBean newsDetailBean = JsonTools.jsonStringToObject(messageJson, MessageListDetailBean.class);
                    tv_title.setText(newsDetailBean.getTitle());
                    String date_time = Tools.timedate(String.valueOf(newsDetailBean.getCreateDate() / 1000));
                    tv_time.setText(date_time);
                    tv_classname.setVisibility(View.VISIBLE);
                    tv_classname.setText(newsDetailBean.getStreetName());
                    String Url = Constant.Head_string + newsDetailBean.getContent() + Constant.foot_string;//URL编码数据
                    webView.getSettings().setDefaultTextEncodingName("UTF-8");
                    webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                    webView.getSettings().setLoadWithOverviewMode(false);
                    webView.loadData(Url, "text/html; charset=UTF-8", null);
                }
            }else if(flag.equals(PartyAnounceFragment.class.getSimpleName()))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String messageJson = JsonTools.objectToJsonString(object);
                    PartyBulletinListBean newsDetailBean = JsonTools.jsonStringToObject(messageJson, PartyBulletinListBean.class);
                    tv_title.setText(newsDetailBean.getBulletinTitle());
                    String date_time = Tools.timedate(String.valueOf(newsDetailBean.getCreateTime() / 1000));
                    tv_time.setText(date_time);
                    tv_classname.setVisibility(View.GONE);
                    String Url = Constant.Head_string + newsDetailBean.getBulletinContent() + Constant.foot_string;//URL编码数据
                    webView.getSettings().setDefaultTextEncodingName("UTF-8");
                    webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                    webView.getSettings().setLoadWithOverviewMode(false);
                    webView.loadData(Url, "text/html; charset=UTF-8", null);
                }
            }else if(flag.equals(PartyNewsFragment.class.getSimpleName()))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String messageJson = JsonTools.objectToJsonString(object);
                    PartyNewsListBean newsDetailBean = JsonTools.jsonStringToObject(messageJson, PartyNewsListBean.class);
                    tv_title.setText(newsDetailBean.getTitle());
                    String date_time = Tools.timedate(String.valueOf(newsDetailBean.getCreateTime() / 1000));
                    tv_time.setText(date_time);
                    tv_classname.setVisibility(View.GONE);
                    String Url = Constant.Head_string + newsDetailBean.getContent() + Constant.foot_string;//URL编码数据
                    webView.getSettings().setDefaultTextEncodingName("UTF-8");
                    webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                    webView.getSettings().setLoadWithOverviewMode(false);
                    webView.loadData(Url, "text/html; charset=UTF-8", null);
                }
            }
            else if(flag.equals(PartyLearnListActivity.class.getSimpleName()))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String messageJson = JsonTools.objectToJsonString(object);
                    partyLearnBean = JsonTools.jsonStringToObject(messageJson, PartyLearnBean.class);
                    tv_title.setText(partyLearnBean.getTitle());
                    String date_time = Tools.timedate(String.valueOf(partyLearnBean.getCreateTime() / 1000));
                    tv_time.setText(date_time);
                    tv_classname.setVisibility(View.GONE);
                    String Url = Constant.Head_string + partyLearnBean.getContent() + Constant.foot_string;//URL编码数据
                    webView.getSettings().setDefaultTextEncodingName("UTF-8");
                    webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                    webView.getSettings().setLoadWithOverviewMode(false);
                    webView.loadData(Url, "text/html; charset=UTF-8", null);
                    fragment_zxxq_seecount.setText(partyLearnBean.getLearnNum()+"人已学习");
                    int status=partyLearnBean.getStatus();
                    String usageTime=partyLearnBean.getUsageTime();
                    String learnSchedule=partyLearnBean.getLearnSchedule();
                    int other=Integer.parseInt(usageTime)-Integer.parseInt(learnSchedule);
                    if(other==0)
                    {
                        tv_learn.setText("学习完成");
                    }else {
                        registerCheck = new TimeCount(other*1000, 1000);
                        registerCheck.start();
                    }

                }
            }else if(flag.equals(POST_LEARNSCHEDULE))
            {
            }
        } catch (Exception e) {

        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error, String flag) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.regist_back:
                finish();
                break;
            case R.id.iv_shareicon:
                if(flag.equals(SelectedIndustryFragment.class.getSimpleName()))
                {
                    String url=Constant.INFO_DETAILSHARE+"?id="+idIntent;
                    String content="我正在关注："+titleIntent+"的新闻详情";
                    Tools.showShare(titleIntent,url,content,context);
                }
                break;
        }
    }

    /* 定义一个倒计时的内部类 */
    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {// 计时完毕时触发
            tv_learn.setText("学习完成");
            postLearnSchedule(0,partyLearnBean);
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程显示
            tv_learn.setText("剩余学习时间 "+millisUntilFinished/1000+"s");
            postLearnSchedule((int) millisUntilFinished/1000,partyLearnBean);
        }
    }

    /**
     * 学习进度post请求
     * needTime 需要学习的时间
     */
    private void postLearnSchedule(int needTime,PartyLearnBean partyLearnBean)
    {
        String contentId=partyLearnBean.getId();
        int learnSchedule=Integer.parseInt(partyLearnBean.getUsageTime())-needTime;
        String stringjson=Tools.getSharedPreferencesStrings(Constant.SharePreferencePartyUserInfo,"",context);
        PartyUserBean partyUserBean=JsonTools.jsonStringToObject(stringjson,PartyUserBean.class);
        String learnUser=partyUserBean.getId();
        String recordId=partyLearnBean.getRecordId();
        int status=1;
        contributorPresenter.postLearnSchedule(contentId,learnSchedule+"",learnUser,recordId,status,POST_LEARNSCHEDULE);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(registerCheck!=null) {
            registerCheck.cancel();
            registerCheck=null;
        }
    }
}
